<?
session_start();
include('conn.php');

$fromdt=$_SESSION['fromdt'];
$todt=$_SESSION['todt'];

$date=$_SESSION['todt'];
$day= substr($date,0,2);
$month = substr($date,3,2);
$year = substr($date,6,4);
$todt=$year.'-'.$month.'-'.$day;

$date=$_SESSION['fromdt'];
$day= substr($date,0,2);
$month = substr($date,3,2);
$year = substr($date,6,4);
$fromdt=$year.'-'.$month.'-'.$day;
$x = $_REQUEST['q'];

switch($x)
{
	case '1':	
		$from = $_REQUEST['from'];
		$to = $_REQUEST['to'];
		$ven = $_REQUEST['ven'];
		$ttype = $_REQUEST['ttype'];
		$q1="";
		$q2="";
		$q3="";
		$q4="";
		
		if(strlen($from))
			$q1=" and t1.entrydt >= '$from' ";
		if(strlen($to))
			$q2=" and t1.entrydt <= '$to' ";
		if(strlen($ven))
			$q3=" and custdesc = '$ven' ";
		if(strlen($ttype))
			$q4=" and trancat = '$ttype' ";
		
		//$qry1 = "SELECT * from expensemast where status=0 order by exptranid desc";
		$qry1 = "SELECT opbaltranid,custdesc, opbaldate,t2.custid,trancat,amtpaid,amtreceived,t2.entrydt,t2.enteredby,t2.status,comment from opbalmast t1
		left join customermast t2 on t1.custid = t2.custid where t1.status=0 $q1 $q2 $q3 $q4 order by opbaltranid desc ";
		//echo $qry1;
		$qry = mysql_query($qry1);
		$data = array();
		$i=1;
		while($rows = mysql_fetch_array($qry))
		{
		$date = $rows['trndate'];
			$year= substr($date,0,4);
			$month = substr($date,5,2);
			$day = substr($date,8,2);
			$data[] = array("Sr_NO" => $i++,
							"opbaltranid" => $rows['opbaltranid'],
							"opbaldate" => $rows['opbaldate'],
							"custid" => $rows['custid'],
							"custdesc" =>$rows['custdesc'],
							"trancat" => $rows['trancat'],
							"amtpaid"=> $rows['amtpaid'],
							"amtreceived"=> $rows['amtreceived'],
							"comment"=> $rows['comment'],
							"entrydt"=> $rows['entrydt'],
							"enteredby"=> $rows['enteredby'],
							"status"=> $rows['status']
							);
		}
		echo json_encode($data);
		break;
		
	case '2':
		$qry1 = "SELECT custdesc from opbalmast t1 left join customermast t2 on t1.custid = t2.custid where t1.status=0 order by custdesc;";
		$qry = mysql_query($qry1);
		$data = array();		
		$i=0;
		while($rows = mysql_fetch_array($qry)){
			$item = $rows["custdesc"];			
			if (!in_array($item, $data)){			
				$data[$i]=$item;
				$i++;
			}
		}
		echo json_encode($data);
		break;
		
	case '3':
		$qry1 = "SELECT trancat from opbalmast order by trancat;";
		$qry = mysql_query($qry1);		
		$data = array();		
		$i=0;
		while($rows = mysql_fetch_array($qry)){
			$item = $rows["trancat"];			
			if (!in_array($item, $data)){			
				$data[$i]=$item;
				$i++;
			}
		}
		echo json_encode($data);
		break;
}

?>